package com.example.fragmentdemo.permission;


import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

public class Permission {

    //定义需要申请的权限
    private String[] permissions=new String[]{
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
    };

    public static int requestCode=0;

    private List<String> permissionList=new ArrayList<>();

    public void checkPermission(Activity activity){
        for (String permission : permissions) {
            //遍历到用户没有授予权限的部分报道集合中去
            if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
                permissionList.add(permission);
            }
        }
        //如果有未授予的权限,则进行权限申请
        if (permissionList.size()>0){
            requestPermission(activity);
        }
    }
    //动态申请权限的方法
    public void requestPermission(Activity activity){
        ActivityCompat.requestPermissions(activity,permissionList.toArray(new String[permissionList.size()]),requestCode);

    }

}
